﻿/*
创建人：zhangrj
日期：2022-08-29
描述：热表子卡提前创建主页面js
*/
var page = {
    url: getRootPath() + "/Ashx/ReBiaoConainer/RBContainerAdvanceStartHandler.ashx", //后台处理url,
    //设备分页参数
    pageTotal: 0,
    pageSize: 10,
    pageNumber: 1,
    init: function () {
        page.bind();
    },
    //绑定事件
    bind: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        //列表加载
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'MFGORDERNAME', title: '生产订单', width: 120 },
                 { field: 'PRODUCTBATCH', title: '件号/批次号', width: 200 },
                 { field: 'PRODUCTDESCRIPTION', title: '零件名称', width: 150 },
                 { field: 'PLANQTY', title: '计划', width: 60 },
                 { field: 'WIPQTY', title: '在制', width: 60 },
                 { field: 'WORKFLOWNAMEANDREV', title: '工艺路线及版本', width: 200 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前工序', width: 180 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 80 }
                ]],
            onSelect: function (rowIndex, rowData) {
                page.fillContainerDetail(rowData);
            }
        });
        //详细列表加载
        $("#gdContainerDetail").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'CONTAINERNAME', title: '批次号', width: 200 },
                 { field: 'QTY', title: '在制', width: 80 }
                ]],
            onCheck: function (rowIndex, rowData) {
                page.fillStep(rowData);
            },
            onUncheck: function (rowIndex, rowData) {
                page.fillStep(rowData);
            },
            onCheckAll: function (rowIndex, rowData) {
                page.fillStep(rowData);
            },
            onUncheckAll: function (rowIndex, rowData) {
                page.clearPage(false, false, false, true, true);
            }
        });
        //工序列表加载
        $("#gdStepInfo").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'SHIPSTEPNAME', title: '工序', width: 200 },
                 { field: 'CONTAINERNAME', title: '流水卡号', width: 200 },
                 { field: 'STEPPLANNEDSTARTDATE', title: '计划发出日期', width: 100,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'ISSUBCONTAINER', title: '是否开卡', width: 100 }
                ]]
        });
        //列表分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.clearPage(false, true, true, true, true);
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            page.clearPage(false, true, true, true, true);
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.search();
        });
        //查询信息
        $("#btnSearch").on("click", function () {
            page.clearPage(false, true, true, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            page.clearPage(true, true, true, true, true);
        });
        //新建设备信息
        $("#btnContainerStart").on("click", function () {
            page.clearPage(false, false, false, false, true);
            page.containerStart();
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetContainerList",
            pageSize: page.pageSize,
            pageIndex: page.pageNumber,
            scanContainer: $.trim($("#txtScanContainer").textbox("getValue")),
            scanOrder: $.trim($("#txtScanOrder").textbox("getValue")),
            productName: $.trim($("#txtProductName").textbox("getValue")),
            containerName: $.trim($("#txtContainerName").textbox("getValue")),
            stepName: $.trim($("#txtStepName").textbox("getValue")),
            stepPlannedStartDate: $.trim($("#txtStepPlannedStartDate").datebox("getValue")),
            stepPlannedEndDate: $.trim($("#txtStepPlannedEndDate").datebox("getValue"))
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdContainerInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //填充批次详细
    fillContainerDetail: function (row) {
        var formData = {
            action: "GetContainerDetail",
            mfgOrderName: row.MFGORDERNAME,
            productId: row.PRODUCTID,
            workflowId: row.WORKFLOWID,
            workflowstepId: row.WORKFLOWSTEPID,
            togetherFlag: row.TOGETHERFLAG,
            containerNo: row.CONTAINERNO
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdContainerDetail").datagrid('loadData', { rows: res.DBTable });
            $("#gdContainerDetail").datagrid('selectAll');
        });
    },
    //填充工序
    fillStep: function () {
        var selected = $("#gdContainerDetail").datagrid("getChecked");
        if (selected.length == 0) {
            page.clearPage(false, false, false, true, true);
            return;
        }
        page.post(page.url, { action: "GetContainerStepDetail", params: JSON.stringify(selected) }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdStepInfo").datagrid('loadData', { rows: res.DBTable });
            $("#gdStepInfo").datagrid('selectAll');
        });
    },
    //开卡
    containerStart: function () {
        var selected = $("#gdStepInfo").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("没有选择工序", false);
            return;
        }
        page.post(page.url, { action: "SetStartSession", params: JSON.stringify(selected) }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            //打开弹出页面
            var url = getRootPath() + "/forms/Custom/ReBiao/RbContainerStartPage.html?v=" + new Date().getTime();
            url += "&parentDiv=dg";
            $('#dg').dialog({
                title: '开卡',
                width: 1200,
                height: 750,
                top: 10,
                closed: false,
                cache: false,
                modal: true,
                content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
                //            poPupData: [{ container: container, reportIds: res.ReturnData}], //弹出页面的传参
                onClose: function () {
                    var returnValue = window.returnValue;
                    if (returnValue != null) {
                        if (returnValue.isSuccess == true) {
                            page.clearPage(false, true, true, true, true);
                            page.search();
                        }
                        displayMessage(returnValue.message, returnValue.isSuccess);
                    }
                }
            });
        });
    },
    //清除页面（查询区域，结果区域，批次详细，基本信息区域，是否清空消息）
    clearPage: function (query, list, detail, info, msg) {
        if (query) {
            $("#txtScanContainer").textbox("setValue", ""); //扫描批次号
            $("#txtScanOrder").textbox("setValue", ""); //扫描订单号
            $("#txtProductName").textbox("setValue", ""); //件号
            $("#txtContainerName").textbox("setValue", ""); //批次号
            $("#txtStepName").textbox("setValue", ""); //工序号
        }
        if (list) {
            clearEasyuiDataGrid("gdContainerInfo"); //清空列表
        }
        if (detail) {
            clearEasyuiDataGrid("gdContainerDetail"); //清空列表
        }
        if (info) {
            clearEasyuiDataGrid("gdStepInfo"); //清空列表
        }
        if (msg) {
            clearMessage();
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
}
page.init();